package Filter::Utils;

# $Id: Utils.pm 159 2010-06-28 16:39:31Z dolbanavt $
# $Date: 2010-06-28 19:39:37 +0300 (Пнд, 28 Июн 2010) $
# $HeadURL: https://robosearch.googlecode.com/svn/branches/reborn/lib/Filter/Utils.pm $
# $LastChangedRevision: 345 $

use JIP::ToolSet;
use Carp qw( croak );
use Const::Fast qw( const );
use English qw( -no_match_vars );
use Encode qw( _utf8_on encode decode from_to );
use cyrillic qw( detect convert );
use Exporter::Lite;

our $VERSION = '1.0';

our @EXPORT_OK = qw( decode_name decode_path resolve_content_type_for );

const my $AUDIO    => qr{
    ^(?:mp3|wav|ogg|flac|wma|m3u|ape|lqt|mmf|amr|mid|midi|wv)$
}ixms;

const my $VIDEO    => qr{
    ^(?:avi|mov|vob|wmv|mpeg|mpg|asf|mkv|flv|mpg|m2v|ogm|mp4|divx|qt|vp6|rm)$
}ixms;

const my $IMAGES   => qr{
    ^(?:iso|vcd|nrg|img|cue|fcd|mds|mdf|ccd|cdi|pdi|bwt|bin)$
}ixms;

const my $TEXT     => qr{
    ^(?:pdf|djv|djvu|doc|txt|html|shtml|xhtml|htm|chm|
    tex|mht|hlp|wrj|rtf|xml|ps|ppt|nfo)$
}ixms;

const my $PICTURES => qr{^(?:gif|jpeg|jpg|bmp|tiff|png|ico)$}ixms;

const my $ARCHIVES => qr{
    ^(?:gz|tgz|bz|bz2|zip|rar|arj|tar|czip|ace|cab|
    gzip|lzh|sqx|rs|uue|xxe|ucl|zlib|rpm|deb|7z|pak)$
}ixms;

const my $SOURCES  => qr{
    ^(?:c|h|cpp|hpp|cc|hh|cs|java|jsp|js|py|pl|pm|asp|cgi|php|
    xsl|xslt|sql|tcl|rb)$
}ixms;

const my $WINDOWS_CYRILLIC => 1251;
const my $ISO_CYRILLIC     => 28_585;

sub decode_name {
    my $text = $ARG[0];

    if ( $text !~ m{[^[:ascii:]]}xms ) {
        _utf8_on( $text );
        return $text;
    }

    my $cp = detect( $text );

    if ( not $cp or $cp eq $ISO_CYRILLIC or $cp eq 'utf8' ) {
        _utf8_on( $text );
        return $text;
    }

    $text = convert( $cp, $WINDOWS_CYRILLIC, $text )
        if $cp ne $WINDOWS_CYRILLIC;

    $text = encode( 'utf8', decode( 'cp1251', $text ) );

    from_to( $text, 'utf8', 'UTF-8' ); # strictify UTF
    _utf8_on( $text );

    return $text;
}

sub decode_path {
    return '/' if $ARG[0] eq '/';

    my @dirs = split m{\/}xms, $ARG[0];

    shift @dirs;

    my $path;

    foreach ( @dirs ){ $path .= '/'. decode_name( $ARG ) }

    return $path;
}

sub resolve_content_type_for {
    my( $file_extention ) = $ARG[0] =~ m/([^\.]+)$/xms; # $ARG[0] === file_name

    given ( $file_extention ) {
        when ( $AUDIO )    { return 'audio'; }
        when ( $VIDEO )    { return 'video'; }
        when ( $IMAGES )   { return 'images'; }
        when ( $TEXT )     { return 'text'; }
        when ( $PICTURES ) { return 'pictures'; }
        when ( $ARCHIVES ) { return 'archives'; }
        when ( $SOURCES )  { return 'sources'; }

        default            { return 'other' }
    }
}

1;

__END__

=head1 NAME

Filter::Utils - набор утилит используемых фильтром контента

=head1 SYNOPSIS

    use Filter::Utils qw(
        decode_name
        decode_path
        resolve_content_type_for
    );

    # кодирует текст в utf8
    my $decoded_name = decode_name('tratata');

    # кодирует путь к объекту в utf8
    my $decoded_path = decode_path('/path');

    # определяет тип контента по расширения файла
    say resolve_content_type_for('file.avi'); # video

=head1 DEPENDENCIES

Exporter::Lite (from CPAN)

Const::Fast (from CPAN)

JIP::ToolSet (from base_lib)

=head1 AUTHOR

Vladimir Zhavoronkov, <dolbanavt (at) Google Mail>

http://dolbanavt.livejournal.com/

=head1 LICENSE AND COPYRIGHT

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

=cut
